<script lang="ts" setup>
import {useRoute} from 'vue-router'
import {ref} from "vue"
import {getAdminNewsCategory, getByIdAdminNews, postAdminNews, putAdminNews} from "../serve"
import {ElMessage} from "element-plus"
import JsEditor from "./js-editor.vue"

const
    route = useRoute(),
    fromData = ref({
      id: '',
      "categoryId": null,
      "title": "",
      "content": ''
    }),
    loading = ref(false),
    isAdd = ref(false),
    fromRules = {
      title: [
        {
          required: true,
          min: 1,
          max: 100,
          message: '文章标题长度1-100位',
          trigger: 'trigger',
        },
      ],
      content: [
        {
          required: true,
          min: 1,
          message: '请输入文章内容',
          trigger: 'trigger',
        },
      ]
    },
    froms = ref(),
    inImage = ref(false)
;

const categories = ref<any>([])

function getCategories() {
  getAdminNewsCategory().then((r: any) => {
    categories.value = r
  })
}

getCategories()

function get() {
  if (route.query['id']) {
    loading.value = true
    isAdd.value = false
    getByIdAdminNews(route.query['id']).then((r: any) => {
      fromData.value = r
      loading.value = false
      inImage.value = true
    }).catch(() => loading.value = false)
  } else {
    isAdd.value = true
  }
}

get()

async function change() {
  try {
    await froms.value.validate()
    loading.value = true
    if (isAdd.value) {
      postAdminNews({...fromData.value}).then(() => {
        loading.value = false
        ElMessage({type: 'success', message: '新增成功！'})
      }).catch(() => loading.value = false)
      return
    }
    putAdminNews({...fromData.value}).then(() => {
      ElMessage({type: 'success', message: '修改成功！'})
      get()
    }).catch(() => loading.value = false)
  } catch (e) {
  }
}

</script>

<template>
  <el-scrollbar>
    <el-card v-loading="loading" box element-loading-text="加载中...">
      <template #header>{{ isAdd ? '资讯添加' : '资讯编辑' }}</template>
      <el-form ref="froms" :model="fromData" :rules="fromRules" el-From>
        <el-form-item label="文章标题：" prop="title">
          <el-input :show-word-limit="true"
                    :maxlength="30"
                    v-model="fromData['title']" placeholder="请输入文章标题" type="textarea" :rows="4"/>
        </el-form-item>
        <el-form-item label="文章分类：" prop="categoryId">
          <el-select v-model="fromData['categoryId']" placeholder="请选择文章分类" clearable>
            <el-option v-for="item in categories" :key="item.categoryId" :label="item.name" :value="item.categoryId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="文章内容：" prop="content">
          <js-editor v-model="fromData.content" :Value="fromData.content"
                     :default-config="{placeholder:'请输入文章内容...'}" :open="inImage" Height="400px"
                     style="width: 100%;"/>
        </el-form-item>
        <el-form-item>
          <el-button round style="width: 80%;margin: auto;" type="success" @click="change">立即提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </el-scrollbar>
</template>

<style lang="scss" scoped>
[box] {
  width: 80%;
  margin: auto;
}

[el-From] {
  width: 98%;
  margin: auto;
}
</style>